<template>
    <div class="guanzhu">       
          <van-button
            v-if="articl.is_followed"
            class="follow-btn"
            :loading="loadin"
            round
            size="small"
            @click="cons"
          >已关注</van-button>
           <van-button 
            v-else
            class="follow-btn"
            type="info"
            color="#3296fa"
            :loading="loadin"
            round
            size="small"
            icon="plus"
            @click="cons"
          >关注</van-button>
    </div>
</template>
<script>
import { addFollow,deleteFollow } from '@/api/user.js'
export default {
  data:function(){
    return {
      isflog:true,
      loadin:false
    }
  },
  props:(['articl']),
  methods:{
    async cons(){
      const target = this.articl.aut_id
      this.loadin = true
      try {
        // 已经关注
        if (this.articl.is_followed){                 
          const os = await deleteFollow(target)
          console.log(os)
          if (os.status === 204){
            this.$toast('取关成功') 
            this.loadin = false
          } else {
            this.$toast('取关失败') 
          }                             
        } else {                    
          const guan = await addFollow(target)
          console.log(guan)
          if (guan.status === 200){
            this.$toast('关注成功')
            this.loadin = false 
          } else {
            this.$toast('关注失败')
          }         
        }
        // 更新视图
        this.articl.is_followed = !this.articl.is_followed
      } catch (error) {
        console.log(error.response)
      }         
    }
  },
  created(){
    console.log(this.articl)
  }
}
</script>
